package net.zoneland.knowledge.service.impl;

import com.google.common.collect.Lists;
import java.util.List;
import net.zoneland.knowledge.dao.QaRecordsV2Mapper;
import net.zoneland.knowledge.enums.TotalListEnum;
import net.zoneland.knowledge.model.QaRecordsV2;
import net.zoneland.knowledge.model.QaRecordsV2Example;
import net.zoneland.knowledge.model.StatisticsBo;
import net.zoneland.knowledge.service.QaRecordsV2Service;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * .
 *
 * @author: bcc
 * 2021/6/8
 */
@Service
@Transactional(transactionManager="transactionManager",rollbackFor = Exception.class)
public class QaRecordsV2ServiceImpl implements QaRecordsV2Service {

    @Autowired
    private QaRecordsV2Mapper qaRecordsV2Mapper;

    @Override
    public List<StatisticsBo> queryHotQuestion(String companyDn, String category){
        return qaRecordsV2Mapper.queryHotQuestion(companyDn,"%" + category + "%", TotalListEnum.HOT.getLimit());
    }

    @Override
    public List<StatisticsBo> querySearchQuestion(final String companyDN, final String value) {
        return qaRecordsV2Mapper.querySearchQuestion(companyDN,TotalListEnum.SEARCH.getLimit());
    }

    @Override
    public QaRecordsV2 getById(final String id) {
        return qaRecordsV2Mapper.selectByPrimaryKey(id);
    }

    @Override
    public void update(final QaRecordsV2 qaRecordsV2) {
        qaRecordsV2Mapper.updateByPrimaryKey(qaRecordsV2);
    }

    @Override
    public List<QaRecordsV2> getFirstSessionId(final String userID, final String talkSessionId) {
        final QaRecordsV2Example example = new QaRecordsV2Example();
        example.createCriteria().andReqIdEqualTo(userID).andSessionIdEqualTo(talkSessionId);
        return qaRecordsV2Mapper.selectByExample(example);
    }

    @Override
    public QaRecordsV2 getNewRecordByQueId(final String userId, final String queName, final int finalAnswer) throws Exception {
        final QaRecordsV2Example example = new QaRecordsV2Example();
        QaRecordsV2Example.Criteria criteria = example.createCriteria().
                andReqIdEqualTo(userId).andAnsCodeEqualTo(1).
                andQueStyleIn(Lists.newArrayList("0", "1", "3")).
                andFinalAnswerEqualTo(finalAnswer);
        if(StringUtils.isNotBlank(queName)) {
            criteria.andQueryTextEqualTo(queName);
        }
        example.setOrderByClause("createtime DESC");
        List<QaRecordsV2> recordsV2s = qaRecordsV2Mapper.selectByExampleWithBLOBs(example);
        if(CollectionUtils.isNotEmpty(recordsV2s)) {
            return recordsV2s.get(0);
        }
        return null;
    }
}
